package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.IdWorker;
import com.macro.mall.mapper.admin.BasicCollectionTypeMapper;
import com.macro.mall.model.admin.BasicCollectionType;
import com.macro.mall.pojo.CollectionTypeParam;
import com.macro.mall.query.CollectionTypeQuery;
import com.macro.mall.service.SysCollectionTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysCollectionTypeServiceImpl implements SysCollectionTypeService {


    @Autowired
    private BasicCollectionTypeMapper collectionTypeMapper;

    @Autowired
    private IdWorker idWorker;


    /***
     * 查询当前采集分类
     * @param id
     * @return
     */
    @Override
    public CommonResult getCollectionType(String id) {
        BasicCollectionType collectionType = collectionTypeMapper.selectByPrimaryKey(Long.valueOf(id));
        return CommonResult.success(collectionType);
    }


    /****
     * 条件分页查询采集分类
     * @param param
     * @return
     */
    @Override
    public CommonResult getList(CollectionTypeParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        CollectionTypeQuery query = new CollectionTypeQuery();
        BeanUtils.copyProperties(param, query);
        List<BasicCollectionType> collectionTypeList = collectionTypeMapper.selectBySelective(query);
        PageInfo<BasicCollectionType> pageInfo = new PageInfo<>(collectionTypeList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }


    /***
     * 编辑采集分类
     * @param param
     * @return
     */
    @Override
    public CommonResult edit(CollectionTypeParam param) {
        if (param.getId() == null || param.getId().equals("") || param.getId().length() <= 0) {
            //新增
            BasicCollectionType collectionType = new BasicCollectionType();
            BeanUtils.copyProperties(param, collectionType);
            collectionType.setId(idWorker.nextId());
            collectionType.setCreationTime(new Date());
            collectionType.setUpdateTime(new Date());
            int row = collectionTypeMapper.insertSelective(collectionType);
            return result(row, "添加采集分类失败");
        }
        BasicCollectionType collectionType = new BasicCollectionType();
        BeanUtils.copyProperties(param, collectionType);
        collectionType.setId(Long.valueOf(param.getId()));
        collectionType.setUpdateTime(new Date());
        int row = collectionTypeMapper.updateByPrimaryKeySelective(collectionType);
        return result(row, "修改采集分类失败");
    }


    /***
     * 根据id删除采集分类
     * @param id
     * @return
     */
    @Override
    public CommonResult delete(String id) {
        int row = collectionTypeMapper.deleteByPrimaryKey(Long.valueOf(id));
        return result(row, "删除采集分类失败");
    }

    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }
}
